package com.cuanbobox.utils;

import android.text.TextUtils;

import com.cuanbobox.base.App;
import com.cuanbobox.bean.DataBean;
import com.cuanbobox.bean.DataK200Bean;
import com.cuanbobox.bean.DataList;
import com.cuanbobox.bean.EventBusBean;
import com.cuanbobox.bean.InOutBean;
import com.cuanbobox.bean.MatrixPortBean;
import com.cuanbobox.bean.MatrixSlefCheckBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 输入输出工具类
 */
public class InOutUtil {
    public static long recordOutClickTime = 0;

    /**
     * 判断两个集合是否相等
     *
     * @param oldList 原来的集合数据
     * @param newList 新的集合数据
     */
    public static boolean isEqual(List<InOutBean> oldList, List<InOutBean> newList) {
        if (oldList == null || newList == null) return false;
        if (oldList.size() == newList.size()) {
            for (int i = 0; i < oldList.size(); i++) {
                InOutBean oldBean = oldList.get(i);
                InOutBean newBean = newList.get(i);
                if (!oldBean.getName().equals(newBean.getName())) {
                    return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }

    /**
     * 绑定数据
     *
     * @param oldList 原来的集合数据
     * @param newList 新的集合数据
     */
    public static void setData(List<InOutBean> oldList, List<InOutBean> newList) {
        if (oldList.size() == newList.size()) {
            for (int i = 0; i < oldList.size(); i++) {
                InOutBean oldBean = oldList.get(i);
                InOutBean newBean = newList.get(i);
                oldBean.setCheck(false);
                oldBean.setId(newBean.getId());
                oldBean.setRes(newBean.getRes());
                oldBean.setType(newBean.getType());
                oldBean.setVsw(newBean.getVsw());
            }
        }
    }

    /**
     * 检查
     */
    public static void check() {
        if (recordOutClickTime <= 0) return;
        long nowTime = new Date().getTime();
        long diff = nowTime - recordOutClickTime;
        if (diff > 500) {
            DataList.add(new DataBean("#JSON:video,0."));
            recordOutClickTime = 0;
        }

    }

    /**
     * 解析16c数据
     *
     * @param bean 16c封装的实体类
     */
    public static void parse16C(MatrixSlefCheckBean bean) {
        if (bean == null) return;
        try {
            List<InOutBean> inList = new ArrayList<>();
            List<InOutBean> outList = new ArrayList<>();
            List<MatrixPortBean> portList = bean.getInfo().getPort();
            if (portList != null) {
                for (int i = 0; i < portList.size(); i++) {

                    MatrixPortBean matrixPortBean = portList.get(i);
                    if (TextUtils.isEmpty(matrixPortBean.getName())) continue;
                /*if(matrixPortBean.isFlag()){
                    strFlag=matrixPortBean.getRes();
                }else{
                    strFlag="无信号";
                }*/
                    if (matrixPortBean.getIo() == 0) {

                    } else if (matrixPortBean.getIo() == 1) {//输入信号
                        inList.add(new InOutBean(i + 1, 0, matrixPortBean.getName(), matrixPortBean.getRes(), matrixPortBean.getVsw()));
                    } else if (matrixPortBean.getIo() == 2) {//输出信号
                        outList.add(new InOutBean(i - 7, 1, matrixPortBean.getName(), matrixPortBean.getRes(), matrixPortBean.getVsw()));
                    }
                }
            }
            App.configInfo.setInList(inList);
            App.configInfo.setOutList(outList);
            SharedPreferencesUtil.saveBean(App.getInstance(), App.configInfo);
            EventBus.getDefault().post(new EventBusBean(EventBusBean.REFRUSH_IN_OUT_ADAPTER));
            LogUtil.add("parse16C", bean.toString(), "输入输出");
            LogUtil.add("输入数据", App.configInfo.getInList().toString(), "输入输出");
            LogUtil.add("输出数据", App.configInfo.getOutList().toString(), "输入输出");
        } catch (Exception e) {
            LogUtil.addError("-------------------输入输出解析16C数据异常-------------------");
            LogUtil.addError(e);
            LogUtil.addError("------------------------------------------------------------");
        }
    }

    /**
     * 解析K200数据
     *
     * @param bean K200封装的实体类
     */
    public static void parseK200(DataK200Bean bean) {
        if (bean == null) return;
        try {
            List<InOutBean> inList = new ArrayList<>();
            List<InOutBean> outList = new ArrayList<>();
            int inId = 0;
            int outId = 0;
            for (int i = 0; i < bean.getInfo().getIn().size(); i++) {
                DataK200Bean.InfoBean.InBean inBean = bean.getInfo().getIn().get(i);
                inId++;
                inList.add(new InOutBean(inId, 0, inBean.getName(), inBean.getRes(), 0));
            }
            for (int i = 0; i < bean.getInfo().getOut().size(); i++) {
                DataK200Bean.InfoBean.OutBean outBean = bean.getInfo().getOut().get(i);
                outId++;
                outList.add(new InOutBean(outId, 1, outBean.getName(), outBean.getRes(), outBean.getVsw()));
            }
            App.configInfo.setInList(inList);
            App.configInfo.setOutList(outList);
            SharedPreferencesUtil.saveBean(App.getInstance(), App.configInfo);
            EventBus.getDefault().post(new EventBusBean(EventBusBean.REFRUSH_IN_OUT_ADAPTER));
            LogUtil.add("parseK200", bean.toString(), "锐取输入输出");
            LogUtil.add("输入数据", App.configInfo.getInList().toString(), "锐取输入输出");
            LogUtil.add("输出数据", App.configInfo.getOutList().toString(), "锐取输入输出");
        } catch (Exception e) {
            LogUtil.addError("-------------------输入输出解析K200数据异常-------------------");
            LogUtil.addError(e);
            LogUtil.addError("-------------------------------------------------------------");
        }
    }
}
